package edu.unf.cen6940.web;

import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.ResearchkeywordDAO;

import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Researchkeyword;

import edu.unf.cen6940.service.ResearchkeywordService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Researchkeyword entities
 * 
 */

@Controller("ResearchkeywordController")
public class ResearchkeywordController {

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Researchkeyword entities
	 * 
	 */
	@Autowired
	private ResearchkeywordDAO researchkeywordDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Researchkeyword entities
	 * 
	 */
	@Autowired
	private ResearchkeywordService researchkeywordService;

	/**
	 * Create a new Researchkeyword entity
	 * 
	 */
	@RequestMapping("/newResearchkeyword")
	public ModelAndView newResearchkeyword() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("researchkeyword", new Researchkeyword());
		mav.addObject("newFlag", true);
		mav.setViewName("researchkeyword/editResearchkeyword");

		return mav;
	}

	
	/**
	 * Edit an existing Researchkeyword entity
	 * 
	 */
	@RequestMapping("/editResearchkeyword")
	public ModelAndView editResearchkeyword(@RequestParam Integer researchkeywordIdKey) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(researchkeywordIdKey));
		mav.setViewName("researchkeyword/editResearchkeyword");

		return mav;
	}
	
	/**
	 * Edit an existing Researchkeyword entity
	 * 
	 */
	/*@RequestMapping("/editResearchkeyword")
	public ModelAndView editResearchkeyword(@RequestParam Integer id) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(id));
		mav.setViewName("researchkeyword/editResearchkeyword");

		return mav;
	}
*/
	/**
	 * Show all Facultyresearch entities by Researchkeyword
	 * 
	 */
	@RequestMapping("/listResearchkeywordFacultyresearchs")
	public ModelAndView listResearchkeywordFacultyresearchs(@RequestParam Integer researchKeywordIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(researchKeywordIdKey));
		mav.setViewName("researchkeyword/facultyresearchs/listFacultyresearchs");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Show all Researchkeyword entities
	 * 
	 */
	@RequestMapping("/indexResearchkeyword")
	public String listResearchkeywords(Model model) {

		model.addAttribute("researchkeywords", researchkeywordService.loadResearchkeywords());
		return "researchkeyword/listResearchkeywords";
	}

	/**
	 */
	@RequestMapping("/researchkeywordController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Save an existing Researchkeyword entity
	 * 
	 */
	/*@RequestMapping("/saveResearchkeyword")
	public String saveResearchkeyword(@ModelAttribute Researchkeyword researchkeyword) {
		researchkeywordService.saveResearchkeyword(researchkeyword);
		return "researchkeyword/listResearchkeywords";
	}*/

	
	@RequestMapping("/saveResearchkeyword")
	public ModelAndView saveResearchkeyword(@ModelAttribute Researchkeyword researchkeyword) {
		researchkeywordService.saveResearchkeyword(researchkeyword);
		ModelAndView modelAndView = new ModelAndView("researchkeyword/listResearchkeywords");
		modelAndView.addObject("researchkeywords", researchkeywordService.loadResearchkeywords());
		return modelAndView;
	}
	
	
	/**
	 * Select the child Facultyresearch entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteResearchkeywordFacultyresearchs")
	public ModelAndView confirmDeleteResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId, @RequestParam Integer related_facultyresearchs_facultyResearchId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("facultyresearch", facultyresearchDAO.findFacultyresearchByPrimaryKey(related_facultyresearchs_facultyResearchId));
		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.setViewName("researchkeyword/facultyresearchs/deleteFacultyresearchs");

		return mav;
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/saveResearchkeywordFacultyresearchs")
	public ModelAndView saveResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId, @ModelAttribute Facultyresearch facultyresearchs) {
		Researchkeyword parent_researchkeyword = researchkeywordService.saveResearchkeywordFacultyresearchs(researchkeyword_researchKeywordId, facultyresearchs);

		ModelAndView mav = new ModelAndView();
		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.addObject("researchkeyword", parent_researchkeyword);
		mav.setViewName("researchkeyword/viewResearchkeyword");

		return mav;
	}

	/**
	 * Edit an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/editResearchkeywordFacultyresearchs")
	public ModelAndView editResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId, @RequestParam Integer facultyresearchs_facultyResearchId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearchs_facultyResearchId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.addObject("facultyresearch", facultyresearch);
		mav.setViewName("researchkeyword/facultyresearchs/editFacultyresearchs");

		return mav;
	}

	/**
	 * Select the Researchkeyword entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteResearchkeyword")
	public ModelAndView confirmDeleteResearchkeyword(@RequestParam Integer researchkeywordIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(researchkeywordIdKey));
		mav.setViewName("researchkeyword/deleteResearchkeyword");

		return mav;
	}
	
	
	
	
	/**
	 * Select the Category entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	/*@RequestMapping("/confirmDeleteCategory")
	public ModelAndView confirmDeleteCategory(@RequestParam Integer categoryIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(categoryIdKey));
		mav.setViewName("category/deleteCategory");

		return mav;
	}*/
	
	
	

	/**
	 * View an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/selectResearchkeywordFacultyresearchs")
	//@RequestMapping("/selectResearchkeyword")
	public ModelAndView selectResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId, @RequestParam Integer facultyresearchs_facultyResearchId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearchs_facultyResearchId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.addObject("facultyresearch", facultyresearch);
		mav.setViewName("researchkeyword/facultyresearchs/viewFacultyresearchs");

		return mav;
	}

	/**
	 * Create a new Facultyresearch entity
	 * 
	 */
	@RequestMapping("/newResearchkeywordFacultyresearchs")
	public ModelAndView newResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.addObject("facultyresearch", new Facultyresearch());
		mav.addObject("newFlag", true);
		mav.setViewName("researchkeyword/facultyresearchs/editFacultyresearchs");

		return mav;
	}

	/**
	 * Entry point to show all Researchkeyword entities
	 * 
	 */
	public String indexResearchkeyword() {
		return "redirect:/indexResearchkeyword";
	}

	/**
	 * Select an existing Researchkeyword entity
	 * 
	 */
	
	
	@RequestMapping("/selectResearchkeyword")
	public String selectResearchkeyword(@RequestParam Integer researchkeywordIdKey, Model model) {
		System.out.println("ResearchkeywordController.selectResearchkeyword()=============="+researchkeywordIdKey);
		model.addAttribute("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(researchkeywordIdKey));
		return "researchkeyword/viewResearchkeyword";
	}
		
	
	/*@RequestMapping("/view")
	public void selectResearchkeyword(@RequestParam Integer id, Model model) {
		System.out.println("ResearchkeywordController.selectResearchkeyword()==============");
		model.addAttribute("researchkeyword", researchkeywordDAO.findResearchkeywordByPrimaryKey(id));
		
	}*/

		
	/**
	 * Delete an existing Researchkeyword entity
	 * 
	 */
	@RequestMapping("/deleteResearchkeyword")
	public String deleteResearchkeyword(@RequestParam Integer researchKeywordIdKey) {
		Researchkeyword researchkeyword = researchkeywordDAO.findResearchkeywordByPrimaryKey(researchKeywordIdKey);
		researchkeywordService.deleteResearchkeyword(researchkeyword);
		return "forward:/indexResearchkeyword";
		//return "researchkeyword/deleteResearchkeyword";
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@RequestMapping("/deleteResearchkeywordFacultyresearchs")
	public ModelAndView deleteResearchkeywordFacultyresearchs(@RequestParam Integer researchkeyword_researchKeywordId, @RequestParam Integer related_facultyresearchs_facultyResearchId) {
		ModelAndView mav = new ModelAndView();

		Researchkeyword researchkeyword = researchkeywordService.deleteResearchkeywordFacultyresearchs(researchkeyword_researchKeywordId, related_facultyresearchs_facultyResearchId);

		mav.addObject("researchkeyword_researchKeywordId", researchkeyword_researchKeywordId);
		mav.addObject("researchkeyword", researchkeyword);
		mav.setViewName("researchkeyword/viewResearchkeyword");

		return mav;
	}
}